package com.ittx.dzt.tiktok.service;

import com.ittx.dzt.edi.dto.MergeInfoDto;
import com.ittx.dzt.edi.dto.OrderMergeDto;
import com.ittx.dzt.edi.dto.PlatformApiParamDTO;
import com.ittx.dzt.edi.dto.StoreConfigDTO;
import com.ittx.dzt.sys.response.Result;
import com.ittx.dzt.sys.util.json.JacksonTool;
import com.ittx.dzt.tiktok.constant.TiktokApiConstant;
import com.ittx.dzt.tiktok.handler.AbstractBaseTiktokApiHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 合单确认
 * <p>
 * cy.order.merge
 * /fulfillment/202309/packages/combine （合单，非欧美地区合单发货使用） https://partner.tiktokshop.com/docv2/page/650aa99adefece02be6f07c9?external_id=650aa99adefece02be6f07c9
 * </p>
 *
 * @Author: gywang
 * @Date: 2024-07-22
 */
@Slf4j
@Service
public class TiktokOrderConfirmMergeService extends AbstractBaseTiktokApiHandler<OrderMergeDto> {
    @Override
    public String getTiktokMethod() {
        return TiktokApiConstant.ORDER_MERGE;
    }

    @Override
    public String getLocalApiMethod() {
        return open.constant.PlatformApiConstant.InboundInterface.TIKTOK_ORDER_MERGE_CONFIRM;
    }

    @Override
    public Result doProcess(PlatformApiParamDTO platformApiParamDTO, OrderMergeDto mergeDto) {
        Result<?> validResult = checkShopCipherInfo(platformApiParamDTO);
        if (validResult.hasError()) {
            return validResult;
        }
        StoreConfigDTO storeConfig = platformApiParamDTO.getStoreConfig();
        Map<String, Object> params = new HashMap<>();
        params.put("shop_cipher", getShopCipher(platformApiParamDTO));
        Map<String, Object> reqBody = new HashMap<>();
        List<Map<String, Object>> combinablePackages = new ArrayList<>();
        reqBody.put("combinable_packages", combinablePackages);
        Map<String, Object> merged = new HashMap<>();
        combinablePackages.add(merged);
        merged.put("id", mergeDto.getTiktokNewOrderCode());
        List<String> ids = new ArrayList<>();
        merged.put("order_ids", ids);

        for (MergeInfoDto infoDto : mergeDto.getMergeList()) {
            ids.add(infoDto.getTid());
        }
        return this.executeTiktokApi(getDefaultUrl(storeConfig), storeConfig, params, JacksonTool.toJson(reqBody), platformApiParamDTO.getRequestId(), TiktokApiConstant.ApiHttpMethod.POST);

    }
}
